//
//  GesturePWSetViewController.m
//  carcareIOS
//
//  Created by 王然 on 16/6/28.
//  Copyright © 2016年 chezheng. All rights reserved.
//

#import "GesturePWSetViewController.h"
#import "WRGestureViewController.h"
#import "TraProjectCell.h"
#import "MessageTestGPWViewController.h"
#import "SettingMgr.h"
#import "AlertActionSheetMgr.h"

#define SECTION_HEIGHT (20)

@interface GesturePWSetViewController ()

//关闭手势密码的数组
@property (nonatomic , readonly) NSArray * titleOFFArray;

//打开手势密码的数组
@property (nonatomic , readonly) NSArray * titleOnArray;

@property (nonatomic , strong) UISwitch * gestureOnOffSW;//手势密码是否打开switch

@property (nonatomic , strong) UISwitch * canLookSW; //手势密码是否显示switch

@property (nonatomic , strong) WRGestureViewController * createWRVC;

@property (nonatomic , strong) WRGestureViewController * checkWRVC;

@property (nonatomic , strong) WRGestureViewController * modifyNewWRVC;

//@property (nonatomic , strong) WRGestureViewController * messageCreateWRVC;

@end

@implementation GesturePWSetViewController
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"手势密码";
    
    self.tableView.frame = CGRectMake(0, 0, __MAIN_WIDTH, __MAIN_HEIGHT_64);
    self.tableView.backgroundColor = [Theme backGroundColor];
    self.tableView.scrollEnabled = YES;
    self.tableView.separatorColor = [Theme lineColor];
    
    [self requestData];
    
}
- (void)requestData
{
    __weak __typeof(self) wself = self;
    [SETTINGMGR getGuestureInfoFinish:^() {
        [wself.tableView reloadData];
    }];
}
-(NSArray *)titleOFFArray
{
    return @[@[@"空白"],@[@"手势密码"]];
}
-(NSArray *)titleOnArray
{
    return @[@[@"空白"],@[@"手势密码",@"显示手势轨迹"],@[@"空白"],@[@"修改手势密码"]];
}
#pragma mark - tableView delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return (SETTINGMGR.showGesturePassword ? self.titleOnArray.count : self.titleOFFArray.count);
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return (SETTINGMGR.showGesturePassword ? [self.titleOnArray[section] count] : [self.titleOFFArray[section] count]);
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return (SETTINGMGR.showGesturePassword ? ((indexPath.section == 1 || indexPath.section == 3) ? [TraProjectCell getCellHeight] : SECTION_HEIGHT):(indexPath.section == 1 ? [TraProjectCell getCellHeight] : SECTION_HEIGHT));
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell * cell;
    switch (indexPath.section) {
        case 1:
        {
            static NSString * cellID = @"cellID";
            cell = [tableView dequeueReusableCellWithIdentifier:cellID];
            if (!cell) {
                cell = [[TraProjectCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
            }
            cell.textLabel.text = (SETTINGMGR.showGesturePassword ? self.titleOnArray[indexPath.section][indexPath.row] : self.titleOFFArray[indexPath.section][indexPath.row]);
            ((TraProjectCell *)cell).switchClick.tag = indexPath.row;
            ((TraProjectCell *)cell).switchClick.onTintColor = [Theme mainColor];
            if(indexPath.row == 0)
            {
                self.gestureOnOffSW = ((TraProjectCell *)cell).switchClick;
                ((TraProjectCell *)cell).switchClick.on = SETTINGMGR.showGesturePassword;
            }
            if(indexPath.row == 1)
            {
                self.canLookSW = ((TraProjectCell *)cell).switchClick;
                ((TraProjectCell *)cell).switchClick.on = SETTINGMGR.showCanLookGesturePW;
            }
            
            [((TraProjectCell *)cell).switchClick addTarget:self action:@selector(switchClick:) forControlEvents:UIControlEventTouchUpInside];
            
        }
            break;
        case 3:
        {
            NSString *identifier = @"SettingsCell";
            cell = [tableView dequeueReusableCellWithIdentifier:identifier];
            
            if (!cell) {
                cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1
                                              reuseIdentifier:identifier];
                [[[UIView viewWithFrame:CGRectMake(0, 0, __MAIN_WIDTH, 0.5)] resetBackgroundColor:[Theme lineColor]] setupOnView:cell.contentView];
                [[[UIView viewWithFrame:CGRectMake(0, [TraProjectCell getCellHeight], __MAIN_WIDTH, 0.5)] resetBackgroundColor:[Theme lineColor]] setupOnView:cell.contentView];
                
                [Factory imageViewWithCenter:CGPointMake(__MAIN_WIDTH - 25, [TraProjectCell getCellHeight]/2) image:ImageNamed(@"cell_right") onView:cell.contentView];
                
            }
            
            cell.textLabel.text = (SETTINGMGR.showGesturePassword ? self.titleOnArray[indexPath.section][indexPath.row] : self.titleOFFArray[indexPath.section][indexPath.row]);
            cell.textLabel.font = FONT_TABLE_TITLE;
            cell.textLabel.textColor = [Theme wordColor];

        }
            break;
            //空白cell
        case 0:
        case 2:
        {
            NSString * cellID = @"noCellID";
            cell = [tableView dequeueReusableCellWithIdentifier:cellID];
            if (!cell) {
                cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
                
                UIView * backView = [[[UIView viewWithFrame:CGRectMake(0, 0, __MAIN_WIDTH, SECTION_HEIGHT)] resetBackgroundColor:[Theme backGroundColor]] setupOnView:cell];
                backView.layer.borderColor = [Theme lineColor].CGColor;
                backView.layer.borderWidth = 0.5;
            }
        }
            break;
        default:
            break;
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
- (void)switchClick:(UISwitch *)switchSender
{
    BOOL isSwitchOn = [switchSender isOn];
    switch (switchSender.tag) {
        case 0:
        {
            if (isSwitchOn) {
                __weak __typeof(self) wself = self;
                TipsView * onTipsV = [TipsView textSmallTipsWithTitle:@"" lightTitles:@[@"只有在进入行程页面时",@"需要输入手势密码"]];
                [onTipsV addOKButton:@"我知道了" clickOK:^{
                    [AlertMgr dismiss:TYPE_VIEW_ALERT];
                    
                    [wself gestureOnOffSwichClick:isSwitchOn];
                }];
                [AlertMgr showView:onTipsV];
            }else
            {
                [self gestureOnOffSwichClick:isSwitchOn];
            }
           
        }
            break;
        case 1:
            SETTINGMGR.showCanLookGesturePW = isSwitchOn;
            [self setSwithOnOffUse];
            break;
        default:
            break;
    }
    
}
- (void)gestureOnOffSwichClick:(BOOL)isSwitchOn
{
    SETTINGMGR.showGesturePassword = isSwitchOn;
    [self.tableView reloadData];
    [self setSwithOnOffUse];
}
#pragma mark -如果打开手势密码需要检验是否有设置过手势密码，有则请求，如果没有设置过手势密码需要设置手势密码 如果关闭轨迹开关 需要验证手势密码
- (void)setSwithOnOffUse
{
    //将要打开密码
    if(self.gestureOnOffSW.on)
    {
        if(![SETTINGMGR.gesturePWStr isNotNil])//设置密码
        {
            __weak __typeof(self) wself = self;
            self.createWRVC = [[WRGestureViewController alloc]initWithType:GestureV_Type_Create withPWOut:SETTINGMGR.showCanLookGesturePW withSuccess:^(NSString *passwordStr, BOOL success) {
                if (success) {
                    [SETTINGMGR setGesturePasswordFlag:YES withCanLookGesturePWFLag:SETTINGMGR.showCanLookGesturePW withGesturePWStr:passwordStr];
                    [AlertMgr showShortSuccessTextTips:@[@"手势密码设置成功"]];
                }
            }];
            self.createWRVC.navLeftBtnClick = ^()
            {
                [wself.tableView reloadData];
                [SETTINGMGR setGesturePasswordFlag:NO withCanLookGesturePWFLag:SETTINGMGR.showCanLookGesturePW withGesturePWStr:SETTINGMGR.gesturePWStr];
            };
//            [self.createWRVC navSetupLeftImageName:@"return_icon" action:^{
//                
//            }];
            
            [self.navigationController pushViewController:self.createWRVC animated:YES];
            
        }else
        {
            [SETTINGMGR setGesturePasswordFlag:SETTINGMGR.showGesturePassword withCanLookGesturePWFLag:SETTINGMGR.showCanLookGesturePW withGesturePWStr:SETTINGMGR.gesturePWStr];
        }

    }else//将要关闭
    {
        if ([SETTINGMGR.gesturePWStr isNotNil] ) {
            __weak __typeof(self) wself = self;
            self.checkWRVC = [[WRGestureViewController alloc]initWithType:GestureV_Type_Check withPWOut:SETTINGMGR.showCanLookGesturePW withSuccess:^(NSString *passwordStr, BOOL success) {
                if (success) {
                    [SETTINGMGR setGesturePasswordFlag:NO withCanLookGesturePWFLag:SETTINGMGR.showCanLookGesturePW withGesturePWStr:passwordStr];
                    
                    if(wself.checkWRVC.gestureViewType == GestureV_Type_Create)[AlertMgr showShortSuccessTextTips:@[@"手势密码设置成功"]];
                }else//手势密码验证次数过多导致失败
                {
                    
                    TipsView *tips = [TipsView warnTipsWithTitle:nil lightTitles:@[@"手势密码输入错误次数过多，您可以通过忘记手势密码来验证身份，或5分钟后再试"]];
                    [tips addLeftButton:@"取消" clickLeft:^{
                        [AlertMgr dismiss:TYPE_VIEW_ALERT];
                    } rightButton:@"忘记手势密码" clickRight:^{
                        [AlertMgr dismiss:TYPE_VIEW_ALERT];
                        
                        [wself.checkWRVC.navigationController pushViewController:[[[MessageTestGPWViewController alloc] initWithMainViewType:TYPE_MAIN_VIEW_NORMAL withTestSuccess:^{
                            wself.checkWRVC.gestureViewType = GestureV_Type_Create;
                            wself.checkWRVC.isTraOutOfPW = SETTINGMGR.showCanLookGesturePW;
                            [wself.checkWRVC navSetupLeftImageName:@"" action:nil];
                        }] navSetupBackItemWithIdentification:BACK_ICON] animated:YES];
                    }];
                    [AlertMgr showView:tips];
                }
            }];
            self.checkWRVC.lostPassword = ^()
            {
                [wself.checkWRVC.navigationController pushViewController:[[[MessageTestGPWViewController alloc] initWithMainViewType:TYPE_MAIN_VIEW_NORMAL withTestSuccess:^{
                    wself.checkWRVC.gestureViewType = GestureV_Type_Create;
                    wself.checkWRVC.isTraOutOfPW = SETTINGMGR.showCanLookGesturePW;
                    [wself.checkWRVC navSetupLeftImageName:@"" action:nil];

                }] navSetupBackItemWithIdentification:BACK_ICON] animated:YES];
            };
            self.checkWRVC.savedPassword = SETTINGMGR.gesturePWStr;
            self.checkWRVC.navLeftBtnClick = ^()
            {
                SETTINGMGR.showGesturePassword = YES;
                [wself.tableView reloadData];
                
            };
            [self.navigationController pushViewController:self.checkWRVC animated:YES];

        }
    }
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 1) {
        switch (indexPath.row) {
            case 0://手势密码
                
                break;
            case 1://显示手势轨迹
                
                break;
            default:
                break;
        }
    }else if(indexPath.section == 3 && indexPath.row == 0)//修改手势密码
    {
        
        __weak __typeof(self) wself = self;
        self.modifyNewWRVC = [[WRGestureViewController alloc]initWithType:GestureV_Type_Modify withPWOut:SETTINGMGR.showCanLookGesturePW withSuccess:^(NSString *passwordStr, BOOL success) {
            if (success) {
//                [wself.modifyNewWRVC.navigationController popViewControllerAnimated:YES];
                [SETTINGMGR setGesturePasswordFlag:SETTINGMGR.showGesturePassword withCanLookGesturePWFLag:SETTINGMGR.showCanLookGesturePW withGesturePWStr:passwordStr];
                if(wself.modifyNewWRVC.gestureViewType == GestureV_Type_Create)[AlertMgr showShortSuccessTextTips:@[@"手势密码设置成功"]];
                if(wself.modifyNewWRVC.gestureViewType == GestureV_Type_Modify)[AlertMgr showShortSuccessTextTips:@[@"手势密码修改成功"]];
            }else//手势密码验证次数过多导致失败
            {
                
                TipsView *tips = [TipsView warnTipsWithTitle:nil lightTitles:@[@"手势密码输入错误次数过多，您可以通过忘记手势密码来验证身份，或5分钟后再试"]];
                [tips addLeftButton:@"取消" clickLeft:^{
                    [AlertMgr dismiss:TYPE_VIEW_ALERT];
                } rightButton:@"忘记手势密码" clickRight:^{
                    [AlertMgr dismiss:TYPE_VIEW_ALERT];
                    
                    
                    [wself.modifyNewWRVC.navigationController pushViewController:[[[MessageTestGPWViewController alloc] initWithMainViewType:TYPE_MAIN_VIEW_NORMAL withTestSuccess:^{
                        
                        wself.modifyNewWRVC.gestureViewType = GestureV_Type_Create;
                        wself.modifyNewWRVC.isTraOutOfPW = SETTINGMGR.showCanLookGesturePW;
//                        [wself.modifyNewWRVC navSetupLeftTitle:@"" action:nil];
//                        [wself.modifyNewWRVC.navigationController popViewControllerAnimated:NO];
//                        wself.messageCreateWRVC = [[WRGestureViewController alloc]initWithType:GestureV_Type_Create withPWOut:SETTINGMGR.showCanLookGesturePW withSuccess:^(NSString *passwordStr, BOOL success) {
//                            if (success) {
////                                [wself.messageCreateWRVC.navigationController popViewControllerAnimated:YES];
//                                SETTINGMGR.gesturePWStr = passwordStr;
//                                [SETTINGMGR setGesturePasswordFlag:SETTINGMGR.showGesturePassword withCanLookGesturePWFLag:SETTINGMGR.showCanLookGesturePW withGesturePWStr:passwordStr];
//                                [AlertMgr showShortSuccessTextTips:@[@"手势密码设置成功"]];
//                            }
//                        }];
//                        [wself.messageCreateWRVC navSetupLeftTitle:@"" action:nil];
//                        [wself.navigationController pushViewController:wself.messageCreateWRVC animated:NO];
                    }] navSetupBackItemWithIdentification:BACK_ICON] animated:YES];
                }];
                [AlertMgr showView:tips];

            }
        }];
        self.modifyNewWRVC .lostPassword = ^()
        {
            [wself.modifyNewWRVC.navigationController pushViewController:[[[MessageTestGPWViewController alloc] initWithMainViewType:TYPE_MAIN_VIEW_NORMAL withTestSuccess:^{
                wself.modifyNewWRVC.gestureViewType = GestureV_Type_Create;
                wself.modifyNewWRVC.isTraOutOfPW = SETTINGMGR.showCanLookGesturePW;
//                [wself.modifyNewWRVC navSetupLeftTitle:@"" action:nil];
//                [wself.modifyNewWRVC.navigationController popViewControllerAnimated:NO];
//                wself.messageCreateWRVC = [[WRGestureViewController alloc]initWithType:GestureV_Type_Create withPWOut:SETTINGMGR.showCanLookGesturePW withSuccess:^(NSString *passwordStr, BOOL success) {
//                    if (success) {
////                        [wself.messageCreateWRVC.navigationController popViewControllerAnimated:YES];
//                        SETTINGMGR.gesturePWStr = passwordStr;
//                        [SETTINGMGR setGesturePasswordFlag:SETTINGMGR.showGesturePassword withCanLookGesturePWFLag:SETTINGMGR.showCanLookGesturePW withGesturePWStr:passwordStr];
//                        [AlertMgr showShortSuccessTextTips:@[@"手势密码设置成功"]];
//                    }
//                }];
//                [wself.messageCreateWRVC navSetupLeftTitle:@"" action:nil];
//                [wself.navigationController pushViewController:wself.messageCreateWRVC animated:NO];
            }] navSetupBackItemWithIdentification:BACK_ICON] animated:YES];
        };
        self.modifyNewWRVC.savedPassword = SETTINGMGR.gesturePWStr;
        [self.navigationController pushViewController:self.modifyNewWRVC animated:YES];
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end
